"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

/*
 * MikoPBX - free phone system for small business
 * Copyright © 2017-2021 Alexey Portnov and Nikolay Beketov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <https://www.gnu.org/licenses/>.
 */

/* global globalRootUrl, Form, extension, Image  */
var avatar = {
  // Store reference to the avatar picture element
  $picture: $('#avatar'),
  // Initialize the avatar component
  initialize: function initialize() {
    // Check if the avatar picture source is empty and set a default image
    if (avatar.$picture.attr('src') === '') {
      avatar.$picture.attr('src', "".concat(globalRootUrl, "assets/img/unknownPerson.jpg"));
    } // Bind click event to upload new avatar button


    $('#upload-new-avatar').on('click', function () {
      $('#file-select').click();
    }); // Bind click event to clear avatar button

    $('#clear-avatar').on('click', function () {
      avatar.$picture.attr('src', "".concat(globalRootUrl, "assets/img/unknownPerson.jpg"));
      extension.$formObj.form('set value', 'user_avatar', null);
      extension.$sip_secret.trigger('change');
    }); // Bind change event to file select input

    $('#file-select').on('change', function (e) {
      var image;
      e.preventDefault();
      var dataTransfer = 'dataTransfer' in e ? e.dataTransfer.files : [];
      var images = 'files' in e.target ? e.target.files : dataTransfer; // Process selected images

      if (images && images.length) {
        Array.from(images).forEach(function (curImage) {
          if (_typeof(curImage) !== 'object') return; // Create new image element and load selected image

          image = new Image();
          image.src = avatar.createObjectURL(curImage);

          image.onload = function (event) {
            var args = {
              src: event.target,
              width: 200,
              height: 200,
              type: 'image/png',
              compress: 90
            }; // Resize and crop the image

            var mybase64resized = avatar.resizeCrop(args); // Update avatar picture source

            avatar.$picture.attr('src', mybase64resized); // Update form value and trigger change event

            extension.$formObj.form('set value', 'user_avatar', mybase64resized);
            extension.$sip_secret.trigger('change');
          };
        });
      }
    });
  },
  // Resize and crop the image
  resizeCrop: function resizeCrop(_ref) {
    var src = _ref.src,
        width = _ref.width,
        height = _ref.height,
        type = _ref.type,
        compress = _ref.compress;
    var newWidth = width;
    var newHeight = height;
    var crop = newWidth === 0 || newHeight === 0; // Skip resizing if the image is smaller than the desired size

    if (src.width <= newWidth && newHeight === 0) {
      newWidth = src.width;
      newHeight = src.height;
    } // Calculate new height while maintaining aspect ratio


    if (src.width > newWidth && newHeight === 0) {
      newHeight = src.height * (newWidth / src.width);
    } // Calculate scale factors


    var xscale = newWidth / src.width;
    var yscale = newHeight / src.height;
    var scale = crop ? Math.min(xscale, yscale) : Math.max(xscale, yscale); // Create an empty canvas element

    var canvas = document.createElement('canvas');
    canvas.width = newWidth || Math.round(src.width * scale);
    canvas.height = newHeight || Math.round(src.height * scale);
    canvas.getContext('2d').scale(scale, scale); // Crop the image to the top center

    canvas.getContext('2d').drawImage(src, (src.width * scale - canvas.width) * -0.5, (src.height * scale - canvas.height) * -0.5);
    return canvas.toDataURL(type, compress);
  },
  createObjectURL: function createObjectURL(i) {
    var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
    return URL.createObjectURL(i);
  }
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9FeHRlbnNpb25zL2V4dGVuc2lvbi1tb2RpZnktYXZhdGFyLmpzIl0sIm5hbWVzIjpbImF2YXRhciIsIiRwaWN0dXJlIiwiJCIsImluaXRpYWxpemUiLCJhdHRyIiwiZ2xvYmFsUm9vdFVybCIsIm9uIiwiY2xpY2siLCJle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LmZyb20oaW1hZ2VzKS5mb3JFYWNoKChjdXJJbWFnZSkgPT4ge1xuICAgICAgICAgICAgICAgICAgICBpZiAodHlwZW9mIGN1ckltYWdlICE9PSAnb2JqZWN0JykgcmV0dXJuO1xuXG4gICAgICAgICAgICAgICAgICAgIC8vIENyZWF0ZSBuZXcgaW1hZ2UgZWxlbWVudCBhbmQgbG9hZCBzZWxlY3RlZCBpbWFnZVxuICAgICAgICAgICAgICAgICAgICBpbWFnZSA9IG5ldyBJbWFnZSgpO1xuICAgICAgICAgICAgICAgICAgICBpbWFnZS5zcmMgPSBhdmF0YXIuY3JlYXRlT2JqZWN0VVJMKGN1ckltYWdlKTtcbiAgICAgICAgICAgICAgICAgICAgaW1hZ2Uub25sb2FkID0gKGV2ZW50KSA9PiB7XG4gICAgICAgICAgICAgICAgICAgICAgICBjb25zdCBhcmdzID0ge1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNyYzogZXZlbnQudGFyZ2V0LFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdpZHRoOiAyMDAsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgaGVpZ2h0OiAyMDAsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgdHlwZTogJ2ltYWdlL3BuZycsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgY29tcHJlc3M6IDkwLFxuICAgICAgICAgICAgICAgICAgICAgICAgfTtcblxuICAgICAgICAgICAgICAgICAgICAgICAgLy8gUmVzaXplIGFuZCBjcm9wIHRoZSBpbWFnZVxuICAgICAgICAgICAgICAgICAgICAgICAgY29uc3QgbXliYXNlNjRyZXNpemVkID0gYXZhdGFyLnJlc2l6ZUNyb3AoYXJncyk7XG5cbiAgICAgICAgICAgICAgICAgICAgICAgIC8vIFVwZGF0ZSBhdmF0YXIgcGljdHVyZSBzb3VyY2VcbiAgICAgICAgICAgICAgICAgICAgICAgIGF2YXRhci4kcGljdHVyZS5hdHRyKCdzcmMnLCBteWJhc2U2NHJl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